package Reference;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.client.MongoCollection;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.bson.Document;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class 独董意见20190101_20190430 {
    public static String [] chineseNames={"secCode","title","secName","date","no","Size","i","title_name"};
    static MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
    static MongoClientOptions options = builder.maxConnectionIdleTime(100000).build();
    static MongoClient mongoClient = new MongoClient("192.168.1.171:30000", options);
    static MongoCollection<Document> collection = mongoClient.getDatabase("JuChaoZiXun").getCollection("独董意见20190101_20190430");

    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient()
                .newBuilder().connectTimeout(60, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).build();
        String Save_Path = null;
        //标题与网址的键值对
        Map<String, String> title_url = new LinkedHashMap<String, String>();
        int count = 1;
        for (int i = 1391; i <1392 ; i++) {//1390
            //对网址进行请求
            String original_url = "http://www.cninfo.com.cn/new/fulltextSearch/full?searchkey=%E7%8B%AC%E8%91%A3%E6%84%8F%E8%A7%81&sdate=2019-01-01&edate=2019-04-30&isfulltext=false&sortName=pubdate&sortType=desc&pageNum="+i+"";

            Request request = new Request.Builder()
                    .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36")
                    .addHeader("Referer", "http://www.cninfo.com.cn/new/fulltextSearch?notautosubmit=&keyWord=%E7%8B%AC%E8%91%A3%E6%84%8F%E8%A7%81")
                    .addHeader("Host", "www.cninfo.com.cn")
//                    .addHeader("Cookie", "JSESSIONID=426EF7D155FA05A206D1ADA4ED153E9E; UC-JSESSIONID=EAD6BFA548B9669B8BA3305859093456; _sp_ses.2141=*; _sp_id.2141=0c51cc29-dd81-44da-866b-7561bc37c311.1586394802.24.1592295928.1590923008.9066da0d-9c5a-4494-8246-c2ba10ac1cbf")
                    .addHeader("Connection", "keep-alive")
                    .addHeader("X-Requested-With", "XMLHttpRequest")
                    .addHeader("sortType","desc")
                    .url(original_url)
                    .build();
            Response response = client.newCall(request).execute();
            byte result[] = response.body().bytes();
            String callback = new String(result, "UTF-8");
            JSONObject jsonObject = JSONObject.parseObject(callback);
            JSONArray jsonArray = jsonObject.getJSONArray("announcements");
            for (int j = 0; j < jsonArray.size(); j++) {
                JSONObject announcement = jsonArray.getJSONObject(j);
                String secCode = announcement.getString("secCode");
                String title = announcement.getString("announcementTitle");
                String secName = announcement.getString("secName");
                String announcementTime = announcement.getString("announcementTime");
                String announcementId = announcement.getString("announcementId");
                String adjunctSize=announcement.getString("adjunctSize");
                title = title.replace("*ST", "ST").replace(":", "：").replace("\"", "").replace("</em>","").replace("<em>","").replace("<","（").replace(">","）");
                //获得网址
                //String adjunctUrl = announcement.getString("adjunctUrl");
                String title_name="";
                client = new OkHttpClient();
                List<Object> dataValueList = new ArrayList<Object>();

                String date =announcementTime;
                String no = announcementId;
                //保存的PDF标题设置，避免重复
                String Size=adjunctSize;
                title_name = secCode + "_"  + title + "_" +secName+  "_" + date + "_" + no+ "_" + Size+ "_第" + i+ "页";
                String download_url = "http://www.cninfo.com.cn/cninfo-new/disclosure/fulltext/download/" + no + "?announceTime=" + date;
                title_url.put(title_name, download_url);
                System.out.println("读取第---"+(j+1)+"--页数据，第--"+(count++)+"--条数据，文件名："+title_name);
                dataValueList.add(secCode);
                dataValueList.add(title);
                dataValueList.add(secName);
                dataValueList.add(date);
                dataValueList.add(no);
                dataValueList.add(Size);
                dataValueList.add(i);
                dataValueList.add(title_name);
                saveData(dataValueList, chineseNames, collection);
            }

        }

    }
    public static void saveData(List<Object> dataList, String[] chineseNames, MongoCollection mongoConnection){
        Document document=new Document();
        for(int i=0;i<dataList.size();i++){
            document.append(chineseNames[i],dataList.get(i));
        }
        mongoConnection.insertOne(document);
    }
}
